package com.pioneer.web.manager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pioneer.common.utils.SecurityUtils;
import com.pioneer.web.client.domain.CliAddress;
import com.pioneer.web.client.service.ICliAddressService;
import com.pioneer.web.manager.domain.ManDeal;
import com.pioneer.web.manager.domain.ManGoods;
import com.pioneer.web.manager.service.IManDealService;
import com.pioneer.web.manager.mapper.ManDealMapper;
import com.pioneer.web.manager.service.IManGoodsService;
import com.pioneer.web.manager.mapper.ManGoodsMapper;
import com.pioneer.web.system.mapper.SysUserMapper;
import com.pioneer.web.system.domain.SysUser;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author work
* @description 针对表【man_deal】的数据库操作Service实现
* @createDate 2025-05-21 14:06:05
*/
@Service
public class ManDealServiceImpl extends ServiceImpl<ManDealMapper, ManDeal>
    implements IManDealService {
    @Resource
    IManGoodsService goodsService;
    @Resource
    ICliAddressService addressService;
    @Resource
    private ManGoodsMapper goodsMapper;
    @Resource
    private SysUserMapper userMapper;

    @Override
    public List<ManDeal> dealList(ManDeal deal) {
        return getBaseMapper().dealList(deal);
    }
    @Override
    public List<ManDeal> dealList1(ManDeal deal) {
        return getBaseMapper().dealList1(deal);
    }

    @Override
    public boolean addDeal(ManDeal mandeal) {

        mandeal.setClientId(SecurityUtils.getUserId());

        ManGoods goods = goodsService.getById(mandeal.getGoodsId());

        CliAddress address = addressService.getById(mandeal.getAddressId());

        mandeal.setProvince(address.getProvince());
        mandeal.setCity(address.getCity());
        mandeal.setDistrict(address.getDistrict());
        mandeal.setConsignee(address.getConsignee());
        mandeal.setPhone(address.getPhone());


        // 计算总价
        mandeal.setTotalPrice(BigDecimal.valueOf(mandeal.getCount()).multiply(goods.getPrice()));

        // 设置默认状态为待发货
        if (mandeal.getStatus() == null) {
            mandeal.setStatus("2");
        }

        // 保存订单
        return this.save(mandeal);
    }

    @Override
    public boolean manAddDeal(ManDeal manDeal) {
        manDeal.setGoodsId(goodsMapper.selectIdByName(manDeal.getGoodsName()));
        manDeal.setClientId(userMapper.selectUserByUserName(manDeal.getUsername()).getUserId());
        return this.save(manDeal);
    }
}




